{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0a82109e-d6b9-496e-893c-3cdd5bbf1114",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "# 字符串索引和切片\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1f91d2aa-d6fe-475e-8743-3d7be975bc99",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字符串的长度为：\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "greeting_str = 'Hey, James!'\n",
    "# 打印字符串长度\n",
    "print('字符串的长度为：')\n",
    "print(len(greeting_str))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2f8120a8-6277-4928-88f2-76dbe159fbb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字符：H，索引：0\n",
      "字符：e，索引：1\n",
      "字符：y，索引：2\n",
      "字符：,，索引：3\n",
      "字符： ，索引：4\n",
      "字符：J，索引：5\n",
      "字符：a，索引：6\n",
      "字符：m，索引：7\n",
      "字符：e，索引：8\n",
      "字符：s，索引：9\n",
      "字符：!，索引：10\n"
     ]
    }
   ],
   "source": [
    "# 打印每个字符和对应的索引\n",
    "for index, char in enumerate(greeting_str):\n",
    "    print(f\"字符：{char}，索引：{index}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "22944431",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引：0, 字符：H\n",
      "索引：1, 字符：e\n",
      "索引：2, 字符：y\n",
      "索引：3, 字符：,\n",
      "索引：4, 字符： \n",
      "索引：5, 字符：J\n",
      "索引：6, 字符：a\n",
      "索引：7, 字符：m\n",
      "索引：8, 字符：e\n",
      "索引：9, 字符：s\n",
      "索引：10, 字符：!\n"
     ]
    }
   ],
   "source": [
    "for index, char in enumerate(greeting_str):\n",
    "    print(f\"索引：{index}, 字符：{char}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "51de6774-ad23-4f16-a909-0648107fde44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "# 单个字符索引\n",
    "print(greeting_str[0])\n",
    "print(greeting_str[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3b683492-f733-4c94-ab20-d3b8001a86ff",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "!\n",
      "s\n"
     ]
    }
   ],
   "source": [
    "print(greeting_str[-1])\n",
    "print(greeting_str[-2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8dd253fb-a33f-4193-9721-b2eea9eb8449",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hey\n"
     ]
    }
   ],
   "source": [
    "# 切片\n",
    "# 取出前3个字符，索引为0、1、2\n",
    "print(greeting_str[:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "50e36271-dab2-4049-b4da-ff4d577a640c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ey, J\n"
     ]
    }
   ],
   "source": [
    "# 取出索引1、2、3、4、5，不含0，不含6\n",
    "print(greeting_str[1:6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c2e683ca-7473-4ac2-ad87-950b4b53ff68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hy ae!\n"
     ]
    }
   ],
   "source": [
    "# 指定步长2，取出第0、2、4 ...\n",
    "print(greeting_str[::2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c3d05ea4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hy ae!\n"
     ]
    }
   ],
   "source": [
    "print(greeting_str[::2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "73f0cbad-dfd5-44e8-bf43-bb33e75bb0c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "!semaJ ,yeH\n"
     ]
    }
   ],
   "source": [
    "# 指定步长-1，倒序\n",
    "print(greeting_str[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c0de61a2-cd5a-4993-b7a6-3d2982d2e627",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
